﻿using huqiang;
using huqiang.Helper.HGUI;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(UIContext), true)]
public class UIElementEditor : Editor
{
    HCanvas FindHCanvas(Transform trans)
    {
        if (trans == null)
            return null;
        var can = trans.GetComponent<HCanvas>();
        if (can == null)
            return FindHCanvas(trans.parent);
        return can;
    }
    public void OnSceneGUI()
    {
        DrawBorder(target as UIContext);
    }
    public void DrawBorder(UIContext txt)
    {
        if (txt == null)
            return;
        Handles.color = Color.red;
        Vector3[] verts = new Vector3[8];
        var matrix = txt.transform.localToWorldMatrix;
        var ui = txt.GetUIData();
        var size = ui.SizeDelta;
        var bor = new Border(size, ui.Pivot);
        var lt = matrix.MultiplyPoint(new Vector3(bor.left, bor.top, 0));
        var rt = matrix.MultiplyPoint(new Vector3(bor.right, bor.top, 0));
        var rd = matrix.MultiplyPoint(new Vector3(bor.right, bor.down, 0));
        var ld = matrix.MultiplyPoint(new Vector3(bor.left, bor.down, 0));
        verts[0] = lt;
        verts[1] = rt;
        verts[2] = rt;
        verts[3] = rd;
        verts[4] = rd;
        verts[5] = ld;
        verts[6] = ld;
        verts[7] = lt;
        Handles.DrawLines(verts);
    }
    public override void OnInspectorGUI()
    {
        var ui = target as UIContext;
        var trans = ui.transform;
        base.OnInspectorGUI();
        var dat = ui.GetUIData();
        dat.activeSelf = ui.gameObject.activeSelf;
        dat.localPosition = trans.localPosition;
        dat.localRotation = trans.localRotation;
        dat.localScale = trans.localScale;
    }
}